package com.css.prompt.sdk.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONObject;
import org.springframework.stereotype.Component;

import com.css.prompt.sdk.config.AiTestAgenterProperties;

@Component
public class AITestAgenterApiClient {
	
    private final String baseUrl;

    public AITestAgenterApiClient(AiTestAgenterProperties properties) {
        this.baseUrl = properties.getBaseUrl();
    }

    /**
     * 调用 /api/generate_test_script 接口
     * @param engine 引擎类型
     * @param userInput 用户输入
     * @return 返回接口响应内容
     */
    public String generateTestScript(String engine, String userInput) {
        JSONObject data = new JSONObject();
        data.set("engine", engine);
        data.set("user_input", userInput);
        HttpResponse response = HttpRequest.post(baseUrl + "/api/generate_test_script")
                .header("Content-Type", "application/json")
                .body(data.toString())
                .execute();
        if (response.getStatus() == HttpStatus.HTTP_OK) {
            return response.body();
        }
        throw new RuntimeException("请求失败: " + response.getStatus());
    }

    /**
     * 调用 /api/validate-robot 接口
     * @param content 机器人内容
     * @return 返回接口响应内容
     */
    public String validateRobot(String content) {
        JSONObject data = new JSONObject();
        data.set("content", content);
        HttpResponse response = HttpRequest.post(baseUrl + "/api/validate-robot")
                .header("Content-Type", "application/json")
                .body(data.toString())
                .execute();
        if (response.getStatus() == HttpStatus.HTTP_OK) {
            return response.body();
        }
        throw new RuntimeException("请求失败: " + response.getStatus());
    }
} 